<?php 
class ownerShopModel extends CDBModelBase {
	public function getTabelName(){
		return "t_owner_shop";
	}

	public function getPKID(){
		return "os_id";
	}
	
	protected function getSequence()
	{
		return "OWNER_SHOP_SEQUENCE";
	}
	
	protected $_dateColumns="cost_time";
	
	/**
	 * 获取随机日期
	 */
	private function generateDate()
	{
		$s_time_str = strtotime('2013-06-01');
		
		$add_time = rand(1000,10000000);
		
		$s_time_str = $s_time_str+$add_time;
		return date("Y-m-d H:i:s",$s_time_str);
	}
	
	/**
	 * 增加积分
	 * @param array $ownerInfo
	 * @param array $ownerShopRe
	 * @param int $ownerID
	 * @param int $shopID
	 * @param decimal $balance
	 * @param int $discountType
	 * @param decimal $discount
	 */
	public function addBalance($ownerInfo,$ownerShopRe,$ownerID,$shopID,$balance,$discountType,$discount,$isTest=false)
	{
		if($isTest==false)
		{
			$this->db->beginTransaction();
		}
		
		$o_balance = $balance;
		
		if($discountType==1)
		{
			$balance += $discount;
		}
		
		if($ownerShopRe)
		{
			$osInfo["os_id"] = $ownerShopRe["os_id"];
			$osInfo["balance"] = $ownerShopRe["balance"]+$balance;
			$osInfo["c_discount_type"] = $discountType;
			$osInfo["c_discount"] = $discount;
				
			$this->updateByPkId($osInfo);
		}
		else{
			$ownerShopRe["balance"] = 0;
			$osInfo["owner_id"] = $ownerID;
			$osInfo["shop_id"] = $shopID;
			$osInfo["balance"] = $balance;
			$osInfo["c_discount_type"] = $discountType;
			$osInfo["c_discount"] = $discount;
			$osInfo["create_time"] = date("Y-m-d H:i:s");
			$this->insert($osInfo);
		}
		$this->db->executeNonQuery("update t_owner set balance=balance+".$balance." where owner_id=".$ownerID);

		$sql = "select CHANGE_SEQUENCE.NEXTVAL from dual";

		$c_id = $this->db->executeScalar($sql);

		$changeInfo["c_id"] = $c_id;
		$changeInfo["pay_id"] = $this->getPayID($isTest,$c_id);
		$changeInfo["owner_id"] = $ownerID;
		$changeInfo["shop_id"] = $shopID;
		$changeInfo["cost_money"] = $o_balance;
		$changeInfo["cost_money_t"] = $balance;
		$changeInfo["cost_type"] = 1;
		$changeInfo["cost_time"] = $isTest?$this->generateDate():$this->getSysDate();
		$changeInfo["owner_card"] = $ownerInfo["supplementary_card"];
		$changeInfo["cost_before_money"] = $ownerInfo["balance"];
		$changeInfo["cost_after_money"] = $ownerInfo["balance"]+$balance;
		$changeInfo["cost_shop_before_money"] = $ownerShopRe["balance"];
		$changeInfo["cost_shop_after_money"] = $ownerShopRe["balance"]+$balance;
		$changeInfo["discount_type"] = $discountType;
		$changeInfo["discount_num"] = $discount;
		
		$this->insert($changeInfo,false,"t_integral_change",false);

		if($isTest==false)
		{
			$this->db->commit();
		
			return $this->db->commitSuccess()?$c_id:0;
		}
	}
	
	private function showMis()
	{
		$t1 = microtime(true);
		echo $t1*1000;
		echo "<br />";
	}
	
	/**
	 * 扣减积分
	 * @param array $ownerInfo
	 * @param array $ownerShopRe
	 * @param int $ownerID
	 * @param int $shopID
	 * @param deciaml $balance
	 * @param deciaml $s_balance
	 */
	public function reduceBalance($ownerInfo,$ownerShopRe,$ownerID,$shopID,$balance,$s_balance,$supplierID,$isTest=false)
	{
		if($isTest==false)
		{
			$this->db->beginTransaction();
		}
		//更新t_owner_shop表的余额
		$osInfo["os_id"] = $ownerShopRe["os_id"];
		$osInfo["balance"] = $ownerShopRe["balance"]-$s_balance;
		$this->updateByPkId($osInfo);

		$a = $this->db->executeNonQuery("update t_owner set balance=balance-".$s_balance." where owner_id=".$ownerID);

		$sql = "select CHANGE_SEQUENCE.NEXTVAL from dual";

		$c_id = $this->db->executeScalar($sql);

		$changeInfo["c_id"] = $c_id;
		$changeInfo["pay_id"] = $this->getPayID($isTest, $c_id);
		$changeInfo["owner_id"] = $ownerID;
		$changeInfo["shop_id"] = $shopID;
		$changeInfo["cost_money"] = $balance;
		$changeInfo["cost_money_t"] = $s_balance;
		$changeInfo["cost_type"] = 2;
		$changeInfo["cost_time"] = $isTest?$this->generateDate():$this->getSysDate();
		$changeInfo["owner_card"] = $ownerInfo["supplementary_card"];
		$changeInfo["cost_before_money"] = $ownerInfo["balance"];
		$changeInfo["cost_after_money"] = $ownerInfo["balance"]-$s_balance;
		$changeInfo["cost_shop_before_money"] = $ownerShopRe["balance"];
		$changeInfo["cost_shop_after_money"] = $ownerShopRe["balance"]-$s_balance;
		$changeInfo["discount_type"] = $ownerShopRe["c_discount_type"];
		$changeInfo["discount_num"] = ($ownerShopRe["c_discount_type"]==1?0:$ownerShopRe["c_discount"]);
		$changeInfo["s_id"] = $supplierID;
		
		$this->insert($changeInfo,false,"t_integral_change",false);
		
		if($isTest==false)
		{
			$this->db->commit();
			
			return $this->db->commitSuccess()?$c_id:0;
		}
	}
	
	public function cancelPay($cInfo,$isTest=false)
	{
		if($isTest==false)
		{
			$this->db->beginTransaction();
		}
		$costType = $cInfo["cost_type"];
		
		$s_balance = $cInfo["cost_money_t"];
		if($costType==1)
		{
			$s_balance = -$s_balance;
		}
		
		$ownerID = $cInfo["owner_id"];
		$shopID = $cInfo["shop_id"];
		
		$ownerInfo = $this->selectByWhere("t_owner","*",array(),"owner_id=".$ownerID);
		
		$osInfo = $this->selectByWhere("t_owner_shop","*",array(),"owner_id=".$ownerID." and shop_id=".$shopID);
		
		//更新t_owner_shop表的余额
		$this->db->executeNonQuery(
		"update t_owner_shop set balance=balance+".$s_balance." 
		where shop_id=".$cInfo["shop_id"]." and owner_id=".$cInfo["owner_id"]);
		
		$this->db->executeNonQuery("update t_owner set balance=balance+".$s_balance." where owner_id=".$cInfo["owner_id"]);
		
		$sql = "select CHANGE_SEQUENCE.NEXTVAL from dual";

		$c_id = $this->db->executeScalar($sql);
		
		$changeInfo["c_id"] = $c_id;
		$changeInfo["pay_id"] = $this->getPayID($isTest,$c_id);
		$changeInfo["owner_id"] = $cInfo["owner_id"];
		$changeInfo["shop_id"] = $cInfo["shop_id"];
		$changeInfo["cost_money"] = abs($s_balance);
		$changeInfo["cost_money_t"] = abs($s_balance);
		$changeInfo["cost_type"] = 3;
		$changeInfo["cost_time"] = $isTest?$this->generateDate():$this->getSysDate();
		$changeInfo["owner_card"] = $cInfo["owner_card"];
		$changeInfo["cost_before_money"] = $ownerInfo["balance"];
		$changeInfo["cost_after_money"] = $ownerInfo["balance"]+$s_balance;
		$changeInfo["cost_shop_before_money"] = $osInfo["balance"];
		$changeInfo["cost_shop_after_money"] = $osInfo["balance"]+$s_balance;
		$changeInfo["discount_type"] = $cInfo["discount_type"];
		$changeInfo["discount_num"] = $cInfo["discount_num"];
		$changeInfo["cancel_pay_id"] = $cInfo["pay_id"];
		$changeInfo["cancel_cost_type"] = $cInfo["cost_type"];
		
		$this->insert($changeInfo,false,"t_integral_change",false);
		
		$this->db->executeNonQuery("update t_integral_change set is_cancel=1 where pay_id='".$cInfo["pay_id"]."'");
		
		if($isTest==false)
		{
			$this->db->commit();
		
			return $this->db->commitSuccess()?$c_id:0;
		}
	}
	
	private function getPayID($isTest,$cid)
	{
		if($isTest)
		{
			return date("YmdHis").$cid;
		}
		$ms = explode(" ",microtime());
		$ms = $ms[0];
		$ms = substr($ms,2,3);
		$id=date("YmdHis".$ms);
		
		$id .= rand(0,9);
		
		$sql = "select pay_id from T_INTEGRAL_CHANGE where pay_id='".$id."'";

		$obj = $this->db->executeScalar($sql);
		
		if($obj)
		{
			return $this->getPayID();
		}
		
		return $id;
	}
}
?>
